﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BaoJianSoft.Common;
using BaoJianSoft.Common.CodeDom;
using BaoJianSoft.ExtensionModel.Statistic;
using BaoJianSoft.ExtensionModel.Web.Properties;
using BaoJianSoft.RapidWeb;
using BaoJianSoft.RapidWeb.Controls;
using BaoJianSoft.RapidWeb.DynamicPages;

namespace BaoJianSoft.ExtensionModel.Web.DynamicPage
{
	/// <summary>
	/// The dynamic page handler to generate statistic report by the statistic definition and query results queried from query panel.
	/// </summary>
	public abstract class StatisticReportDynamicPage : BaoJianSoft.RapidWeb.DynamicPages.DynamicPage
	{
		private static object syncObject = new object();
		private static IStatisticApi statisticApi = SpringContext.Current.GetObject<IStatisticApi>();

		/// <summary>
		/// Execute query for statistic report.
		/// The method will get statistic report original data from abstract method "GetStatisticReportDataSource" first. 
		/// Then analyze the data by the statistic definition to generate statistic report and transform into query result to client.
		/// </summary>
		/// <param name="parameter">Query parameter.</param>
		/// <returns>Returns query results.</returns>
		public override QueryResults Query(QueryParameter parameter)
		{
			QueryResults dataSource = this.GetStatisticReportDataSource(parameter);
			StatisticDefinition statisticDefinition = this.GetStatisticDefinition();
			IEnumerable statisticReportDataSource = statisticApi.CreateStatisticReport(statisticDefinition, dataSource.Results.Cast<object>().ToList());
			return new QueryResults(statisticReportDataSource.Cast<object>().Count(), statisticReportDataSource);
		}

		/// <summary>
		/// Setup StatisticDefinition for StatisticReportGridColumnConfigurationProcessCallback potentially.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public override void SetupContextTempVariables(object sender, SetupApplicationContextVariablesEventArgs e)
		{
			StatisticDefinition statisticDefinition = this.GetStatisticDefinition();
			e.TempVariables["StatisticDefinition"] = statisticDefinition;
			e.TempVariables["StatisticDefinition.Id"] = statisticDefinition.Id;
			e.TempVariables["StatisticDefinition.Name"] = statisticDefinition.Name;
		}

		/// <summary>
		/// The method to get statistic report original data into QueryResults from database or anywhere else by the query parameter submitted from client.
		/// The returned query results from this method will be analyzed by the statistic definition for statistic report. 
		/// </summary>
		/// <param name="parameter"></param>
		/// <returns></returns>
		protected abstract QueryResults GetStatisticReportDataSource(QueryParameter parameter);

		/// <summary>
		/// Get statistic definition used to analyze statistic original record for report.
		/// </summary>
		/// <returns></returns>
		protected abstract StatisticDefinition GetStatisticDefinition();
	}
}
